/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package argtrust.trustnet;

import argtrust.measure.Measure;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Collection;

/**
 *
 * @author Yuqing Tang
 */
public class TrustNet {

    Map<String, Agent> mAgentTable;
    Map<Agent, TrustNode> mTrustTable;

    public TrustNet() {
        mTrustTable = new LinkedHashMap<Agent, TrustNode>();
        mAgentTable = new LinkedHashMap<String, Agent>();
    }

    public TrustNode getTrustNode(Agent agent) {
        TrustNode trust_node = mTrustTable.get(agent);
        if (trust_node == null) {
            trust_node = new TrustNode(agent);
            mTrustTable.put(agent, trust_node);
        }
        return trust_node;
    }

    public TrustNode getTrustNode(String name) {
        return getTrustNode(getAgent(name));
    }

    public Agent getAgent(String name) {
        Agent agent = mAgentTable.get(name);
        if (agent == null) {
            agent = new Agent(name);
            mAgentTable.put(name, agent);
        }
        return agent;
    }
    public boolean hasAgent(String name) {
        Agent agent = mAgentTable.get(name);
        return (agent != null);
    }

    public void trust(String truster, String trustee, Measure measure) {
        trust(getAgent(truster), getAgent(trustee), measure);
    }

    public void trust(Agent truster, Agent trustee, Measure measure) {
        TrustNode truster_node = getTrustNode(truster);

        TrustNode trustee_node = getTrustNode(trustee);
        truster_node.linkTrusteeNode(trustee_node, measure);
    }

    public Set<Agent> getAgentSet() {
        return mTrustTable.keySet();
    }

    public Collection<TrustNode> getTrustNodes() {
        return mTrustTable.values();
    }
}
